// pages/driverView/driverView.js
const ChooseCity = require('../../components/chooseCity/chooseCity.js')
var pickerFile = require('../../components/picker_datetime/picker_datetime.js');
var Api = require('../../config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    depName:"出发地",
    arrvieName:"目的地",
    startDate:"12:00",
    backTime:"0",
    depCount:"4",

    infoList:[
  
      {
        "infoName": "总人数", "infoIcon": "/images/filterIcon.png", "infoType": "picker", 
         },
      { "infoName": "赞助/人", "infoIcon": "/images/th.png", "infoType": "input", "placeText": "0"},
      { "infoName": "备注:", "infoIcon": "/images/wechat_icon.png", "infoType": "text", "placeText": "随便说点什么吧!!" },
    ],
    peopleArray: [1, 2, 3, 4, 5, 6],
    peopleIndex:0,
    
    declarantChecked:true,


    markers: [{
      iconPath: "/images/driverView/depCityIcon.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 30,
      height: 30
    }],

    hasChangeMap:true,
    peopleNum:0,
    sharingMoney:0,
    cycDepTime:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    let driverType = options.driverType
    let driverFun = options.driverFun
    self.chooseCity = new ChooseCity(self)
    self.chooseCity.getAppLoction()
    self.datetimePicker = new pickerFile.pickerDatetime({
      page: self,
      animation: 'slide',
      duration: 500
    }); 
    let pickerPreDate = new Date

    var pickerPreYear = pickerPreDate.getFullYear();
    var pickerPreMonth = pickerPreDate.getMonth() + 1;
    var pickerPreDay = pickerPreDate.getDate();
    var pickerPreHour = pickerPreDate.getHours();
    var pickerPreMinute = pickerPreDate.getMinutes();
    var obj = new Object();
    let startDate = pickerPreYear + '-' + pickerPreMonth + '-' + pickerPreDay + ' ' + pickerPreHour + ':' + pickerPreMinute

    let cycDepTime = pickerPreHour + ':' + pickerPreMinute
    let cycBackTime = cycDepTime
    console.log("open", driverFun, options)
    let naviStr = ''
    if (driverType == 'passenger'){
      naviStr = '乘客'
    }else{
      naviStr = '司机'
    }

    if (driverFun == 'cyc'){
      naviStr = naviStr + ' & 多次'
    }else{
      naviStr = naviStr + ' & 单次'
    }
    wx.setNavigationBarTitle({
      title: naviStr,
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#00e7d3',
    })
    self.setData({
      sendDate:pickerPreDate,
      startDate: startDate,
      driverType: driverType,
      driverFun: driverFun,
      cycDepTime: cycDepTime,
      cycBackTime: cycBackTime,
    })

  },



  appShowLine: function () {
    


  },




 

  depTimeChoose: function () {
    this.datetimePicker.setPicker('startDate');
  },
  


  bindPickerChange: function (event) {
    let peopleIndex = event.detail.value
    this.setData({ peopleIndex: peopleIndex})
  },


  bindinput: function (e) {
    console.log(e.detail.value)
    let tipText = e.detail.value
    this.setData({ tipText: tipText })
  },

  bindKeyInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      sharingMoney: e.detail.value
    })
  },
  bindDepTimeChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      cycDepTime: e.detail.value
    })
  },
 
  bindBackTimeChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      cycBackTime: e.detail.value
    })
  },



  postMyOrder: function () {

    let self = this
    let postUrl = Api.service.postCreateRoute
    let app = getApp()
    let depName = self.data.depName                       //出发地
    let arrvieName = self.data.arrvieName                 //目的地
    let declarantChecked = self.data.declarantChecked     //是否同意免责声明  不传
    var driverType = self.data.driverType //司机或乘客
    if (driverType == "passenger"){
      driverType = 1
    }else{
      driverType = 2
    }
    let driverFunStr = self.data.driverFun
    var driverFun = 0   //一次或N次
    if (driverFunStr == "cyc"){
      driverFun = 2
    }else{
      driverFun = 1
    }

    if (depName == "出发地" || depName == '定位失败,请重试...') {
      wx.showToast({
        image: '/images/noWayWhite.png',
        title: '请选择出发地',
      })
      return
    } else if (arrvieName == "目的地" || arrvieName == '定位失败,请重试...') {
      wx.showToast({
        image: '/images/noWayWhite.png',
        title: '请选择目的地',
      })
      return
    } else if (declarantChecked == false) {
      wx.showToast({
        image: '/images/noWayWhite.png',
        title: '是否查看免责声明',
      })
      return
    }



    let startDate = self.data.startDate                   //出发时间
    let backTime = self.data.cycBackTime                  //返程时间
    //driverType                                          //乘客类型 1司机  2乘客
    //driverFun                                           //循环类型 1单次  2多次
    let peopleIndex = self.data.peopleIndex
    let peopleArray = self.data.peopleArray
    let depCount = self.data.peopleIndex                  //出行人数
    let tipText = self.data.tipText                       //备注
    let sharingMoney = self.data.sharingMoney             //价格
    let cycDepTime = self.data.cycDepTime                 //多次出发时间     调整
    let depAddBean = self.data.depAddBean
    let arrAddBean = self.data.arrAddBean
    let hasChooseDepName = self.data.hasChooseDepName
    var sendDepProvince = ''                              //出发地省名
    var sendDepCity = ''                                  //出发地市名
    let sendArrProvince = arrAddBean.REGION_PROVINCE      //目的地省名
    var sendArrCity = arrAddBean.REGION_CITY              //目的地市区

    let requestHead = app.globalData.requestHead
    if (requestHead.isHasHeadInfo == false) {
      app.selfLogin(self.postInfoTapped)
    }
    let head = requestHead.header
    console.log('head', head)

    if (hasChooseDepName == true){
      sendDepProvince = depAddBean.REGION_PROVINCE
      sendDepCity = depAddBean.REGION_CITY
    }else{
      sendDepProvince = app.globalData.currentProvince 
      sendDepCity = app.globalData.currentCity 
    }


    console.log('depAddBean', depAddBean, arrAddBean)

   

    if (!tipText){
      tipText = '来者不拒'
    }



    var para = { 
      
      departure: depName,
      // dep_province: sendDepProvince,
      departure_city: sendDepCity,
      departure_time: startDate,
      destination: arrvieName,
      // arr_province: sendArrProvince, 
      destination_city: sendArrCity,
      
      // dep_time: cycDepTime,
      // back_time: backTime,

      route_type: driverType,
      // cyc_type: driverFun,
      seat_num: peopleArray[peopleIndex],
      remark: tipText,
      price: sharingMoney,
      total:4,
      
      

      } 

    console.log("para", para)

    wx.request({
      url: postUrl,
      data: para,
      header: head,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        console.log("postres",res)
        let toastStr = res.data.msg
        wx.showToast({
          icon:'success',
          title: toastStr,
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})